package com.eshop.controller;

import com.eshop.pojo.Pager;
import com.eshop.pojo.UserInfo;
import com.eshop.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/userInfo")
public class UserInfoController {
    @Autowired
    private UserInfoService service;

    @RequestMapping("/getValidUser")
    @ResponseBody
    public List<UserInfo> getValidUser(){
        List<UserInfo> userInfoList = service.getValidUser();
        UserInfo userInfo = new UserInfo();
        userInfo.setUserName("请选择...");
        userInfo.setId(0);
        userInfoList.add(userInfo);
        return userInfoList;
    }

    @ResponseBody
    @RequestMapping("/getUsers")
    public Map<String, Object> getAllUser(int page, int rows, UserInfo userInfo){
        Pager pager = new Pager();
        pager.setCurPage(page);
        pager.setPerPageRows(rows);
        Map<String ,Object> map = new HashMap<>();
        map.put("userInfo", userInfo);
        int count = service.count(map);

        List<UserInfo> userInfoList = service.selectByPage(userInfo,pager);
        Map<String ,Object> result = new HashMap<>();
        result.put("total", count);
        result.put("rows", userInfoList);
        return result;
    }

    @ResponseBody
    @RequestMapping(value = "/updateUserStatus",produces = "text/html;charset=UTF-8")
    public String updateUserStatus(@RequestParam("flag")String  flag, @RequestParam("ids")String ids){
        /*String[] id = ids.split(",");
        for (String i : id){
            service.updateStatus(); //之前吧dao层修改过了 直接${ids}
        }*/
        String  count;
        try {
            count = String.valueOf(service.updateStatus(ids,flag));
        } catch (Exception e) {
            e.printStackTrace();
            return "{\"success\":\"false\",\"message\":\"客户状态修改失败！\"}";
        }
        String word = "个用户启用成功!";
        if(flag.equals("1")){
            word = "个用户启用成功！";
        }else {
            word = "个用户禁用成功！";
        }
        return "{\"success\":\"true\",\"message\":\"" + word + "\",\"count\":" + count + "}";
        //return "{\"success\":\"true\",\"message\":\"成功！\",\"count\":" + count + "}";
    }
}
